import * as Cesium from 'cesium';

// 定义区县名称类型
type DistrictName =
  | '起步区'
  | '济阳区'
  | '槐荫区'
  | '平阴县'
  | '天桥区'
  | '莱芜区'
  | '章丘区'
  | '历下区'
  | '市中区'
  | '长清区'
  | '钢城区'
  | '南部山区'
  | '历城区'
  | '高新区'
  | '商河县';

// 定义区县颜色映射类型
type DistrictColors = Record<DistrictName, string>;

// 定义GeoJSON数据结构类型
interface GeoJsonFeature {
  type: 'Feature';
  id: number;
  geometry: {
    type: 'Polygon';
    coordinates: number[][][];
  };
  properties: {
    objectid: number;
    name: DistrictName;
    qhdm: number;
    shi: string;
    uid: number;
    Shape__Area: number;
    Shape__Length: number;
  };
}

interface GeoJsonData {
  type: 'FeatureCollection';
  features: GeoJsonFeature[];
}

export default class JinanDistrictVisualizer {
  private viewer: Cesium.Viewer;
  private dataSources: Cesium.GeoJsonDataSource[];

  constructor(viewer: Cesium.Viewer) {
    this.viewer = viewer;
    this.dataSources = [];
  }

  // 添加区县边界
  addDistricts(geoJsonData: GeoJsonData): void {
    // 清除现有数据
    this.clearDistricts();

    // 定义区县颜色 - 根据实际JSON数据中的名称
    const districtColors: DistrictColors = {
      '起步区': '#FF6B6B',
      '济阳区': '#4ECDC4',
      '槐荫区': '#C7F464',
      '平阴县': '#FFA5A5',
      '天桥区': '#51CF66',
      '莱芜区': '#FCC419',
      '章丘区': '#339AF0',
      '历下区': '#A17DC3',
      '市中区': '#FF9F43',
      '长清区': '#6DBCDB',
      '钢城区': '#ED4C67',
      '南部山区': '#B53471',
      '历城区': '#FF6B9A',
      '高新区': '#4ECDC4',
      '商河县': '#C7F464'
    };

    // 添加每个区县
    geoJsonData.features.forEach((feature: GeoJsonFeature) => {
      const name: DistrictName = feature.properties.name;
      const color: Cesium.Color = Cesium.Color.fromCssColorString(districtColors[name] || Cesium.Color.fromRandom({ alpha: 0.7 }).toCssHexString());

      // 创建数据源
      const dataSource: Cesium.GeoJsonDataSource = new Cesium.GeoJsonDataSource();

      // 加载GeoJSON数据
      dataSource
        .load(feature, {
          fill: Cesium.Color.TRANSPARENT, // 设置为透明，不显示填充
          stroke: color,
          strokeWidth: 2,
          clampToGround: true
        })
        .then(() => {
          // 手动设置边线样式
          dataSource.entities.values.forEach((entity: Cesium.Entity) => {
            if (entity.polygon) {
              // 设置多边形边线
              entity.polygon.outline = new Cesium.ConstantProperty(true);
              entity.polygon.outlineColor = new Cesium.ConstantProperty(color);
              entity.polygon.outlineWidth = new Cesium.ConstantProperty(3);
              entity.polygon.material = new Cesium.ColorMaterialProperty(Cesium.Color.TRANSPARENT);
            }
            if (entity.polyline) {
              // 设置线条样式
              entity.polyline.material = new Cesium.ColorMaterialProperty(color);
              entity.polyline.width = new Cesium.ConstantProperty(3);
            }
          });

          this.viewer.dataSources.add(dataSource);
          this.dataSources.push(dataSource);

          console.log(`成功加载区县: ${name}`);
        })
        .catch((error: Error) => {
          console.error(`加载区县 ${name} 失败:`, error);
        });
    });
  }

  // 清除所有区县边界
  clearDistricts(): void {
    this.dataSources.forEach((dataSource: Cesium.GeoJsonDataSource) => {
      this.viewer.dataSources.remove(dataSource);
    });
    this.dataSources = [];
  }
}
